package com.eliansun.crawler.pageparse.henan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eliansun.crawler.pageparse.AbstractPageProcess;
import net.dongliu.requests.Requests;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * user: lxc
 * data:2019- 07-04-09
 * 优德医药网     youde.com
 */
public class Youde extends AbstractPageProcess {

    int count = 0;
    /**
     *  商品列表界面异步加载数据url
     */
    public static final String POST_URL =  "https://www.youde.com/f/pc/s_10020/goods/list/json";

    /**
     * 访问商品详情界面url(不是详情界面数据加载url)
     */
    public static final String DETAIL_UTL = "https://www.youde.com/s_10020/tggoods?";

    /**
     * 商品详情界面异步加载数据url
     */
    public static final String DETAIL_UTL_DATA = "https://www.youde.com/f/pc/s_10020/goods/ajax/goods";

    @Override
    public void process(Page page) {
        try{
            //列表界面
            if( page.getUrl().toString().contains("tggoodsList") ){
                Map<String,Object> parmes = new HashMap<>();
                parmes.put("keywords",super.wordKey);
                parmes.put("siteId","s_10020");
                String result = Requests.post(POST_URL).params(parmes).send().readToText();
                JSONArray data = JSON.parseObject(result).getJSONArray("data");
                if( data != null){
                    for (int i = 0; i < data.size(); i++) {
                        JSONArray jsonArray = JSON.parseArray(
                                                           JSON.parseObject(
                                                                              data.get(i).toString()
                                                                            ).get("specValueJson")
                                                                            .toString()
                                                              );
                        JSONObject parse = JSON.parseObject(jsonArray.get(0).toString());
                        page.addTargetRequest(DETAIL_UTL  + "goodsId=" + parse.get("goodsId").toString());
                    }
                }


                //获取下一页放到队列中
                Object getTotalPage = JSON.parseObject(JSON.parseObject(result).get("page").toString()).get("pageCount");
                if( getTotalPage != null ){
                    //总页数
                    Integer totalPage =  Integer.parseInt( getTotalPage.toString() ) ;
                    for ( int i = 1; i < totalPage; i++ ) {
                        Map<String,Object> map = new HashMap<String,Object>();
                        parmes.put("keywords",super.wordKey);
                        map.put("siteId","s_10020");
                        map.put("pageIndex",""+(i+1)+"");
                        String resultNext = Requests.post(POST_URL).params(map).send().readToText();
                        JSONArray dataNext = JSON.parseObject(resultNext).getJSONArray("data");
                        for ( int j = 0; j < dataNext.size(); j++ ) {
                            Object specValueJson = JSON.parseObject(dataNext.get(j).toString()).get("specValueJson");
                            JSONArray jsonArray =  JSON.parseArray(specValueJson.toString());
                            JSONObject parse = JSON.parseObject(jsonArray.get(0).toString());
                            page.addTargetRequest(DETAIL_UTL  + "goodsId=" + parse.get("goodsId").toString());
                        }
                    }
                }
            }else{
                //详情界面
                Map<String,Object> map = new HashMap<String,Object>();
                Map<String, Object> resMap = new HashMap<String, Object>();
                String[] split = page.getUrl().toString().split("=");
                if(  split.length>1 ){
                    map.put("id",""+split[1]+"");
                }
                String result = Requests.post(DETAIL_UTL_DATA).params(map).send().readToText();
                Object dataObj = JSON.parseObject(result).get("data");

                if( dataObj != null ){
                    // 商品名称
                    resMap.put("goodsName",JSON.parseObject( dataObj.toString() ).get("title") );
                    resMap.put("price", Integer.parseInt(JSON.parseObject( dataObj.toString() ).get("minPrice").toString()) / 100);
                    //生产企业
                    resMap.put("sccj",page.getHtml().xpath("//*[@id=_goodsMedicinedes]/li[3]/p[2]/text()").get());
                    //批准文号
                    resMap.put("zch",page.getHtml().xpath("//*[@id=_goodsMedicinedes]/li[2]/p[2]/text()").get());
                }
                List<String> titleList = page.getHtml().xpath("//ul[@id='_goodsMedicinedes']//li//p//span//text()").all();
                List<String> detailList = page.getHtml().xpath("//ul[@id='_goodsMedicinedes']//li//p//text()").all();
                String spec = page.getHtml().xpath("//ul[@id='operationUL']//html()").toString();
                String medicalDeviceNo = "";
                String productCompany = "";
                for (int i = 0; i < titleList.size(); i++) {
                    if(titleList.get(i).contains("批准文号")){
                         medicalDeviceNo = detailList.get(i).trim();
                    }
                    if(titleList.get(i).contains("生产厂家")){
                        productCompany = detailList.get(i).trim();
                    }
                }
                Map<String,Object> resultMap = new HashMap<>();
                resultMap.put("spec", spec);
                resultMap.put("medicalDeviceNo", medicalDeviceNo);
                resultMap.put("productCompany", productCompany);
                resultMap.put("price",resMap.get("price"));
                resultMap.put("brand","");
                resultMap.put("productName",resMap.get("goodsName"));
                resultMap.put("executiveStandard","");
                resultMap.put("nationality","");
                resultMap.put("store",super.cTerrace);
                resultMap.put("url",page.getUrl());
                resultMap.put("version","");
                page.putField("result", resultMap);
                count ++;
                System.out.println(count );
            }
        }catch ( Exception e){
               e.printStackTrace();
        }



    }

    Site site = Site.me()
            //设置编码
            .setCharset("utf-8")
            //设置抓取间隔时间
            .setSleepTime(1000*3)
            //设置超时时间，单位毫秒
            .setTimeOut(1000*3)
            //设置重试间隔时间
            .setRetrySleepTime(1000*3)
            //设置重试次数
            .setRetryTimes(3);
    @Override
    public Site getSite() {
        return site;
    }



    public static void main(String[] args) {
        Spider.create( new Youde())
                .addUrl("https://www.youde.com/s_10020/tggoods?goodsId=24")
                //设置多个线程同时抓取数据
                .thread(10)
                .run();

    }

}
